package com.pointlion.sys.mvc.admin.evaluate.paperitem;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.pointlion.sys.mvc.common.model.PaperItem;
import com.pointlion.sys.plugin.shiro.ShiroKit;

import java.util.List;

public class PaperItemService{
	public static final PaperItemService me = new PaperItemService();
	public static final String TABLE_NAME = PaperItem.tableName;
	
	/***
	 * ����������ѯ
	 */
	public PaperItem getById(String id){
		return PaperItem.dao.findById(id);
	}
	
	/***
	 *
	 */
	public Page<Record> getPage(int pnum,int psize){
		String sql  = " from "+TABLE_NAME+" o LEFT JOIN act_hi_procinst p ON o.proc_ins_id=p.ID_  where o.userid='"+ShiroKit.getUserId()+"'  order by o.create_time desc";
		return Db.paginate(pnum, psize, " select * ", sql);
	}
	
	/***
	 *
	 * @param ids
	 */
	@Before(Tx.class)
	public void deleteByIds(String ids){
    	String idarr[] = ids.split(",");
    	for(String id : idarr){
    		PaperItem o = me.getById(id);
    		o.delete();
    	}
	}

    public PaperItem getNameByid(String id) {
		String sql = " select * from paper_base where id = '"+id+"'";
		return PaperItem.dao.findFirst(sql);
    }

	public List<PaperItem> getByPaperId(String id) {
		String sql = "select * from paper_item where paper_id = ?";
		return PaperItem.dao.find(sql,id);
	}

	public void deleteByPaperId(String id) {
		String sql = "delete from paper_item where paper_id = '"+id+"'";
		Db.update(sql);
	}

	public List<PaperItem> getItems(String id) {
		String sql = "select i.* from paper_item i left join paper_base b on b.id = i.base_id where b.parent_id = '#root' and i.paper_id = '"+id+"' ";
		return PaperItem.dao.find(sql);
	}

	public List<PaperItem> getHistoryItems(String id) {
		String sql = "select i.* from paper_user i left join paper_base b on b.id = i.base_id where i.parent_id = '#root' and i.paper_id = '"+id+"' and i.create_id = '"+ShiroKit.getUserId()+"' ";
		return PaperItem.dao.find(sql);
	}

	public List<PaperItem> getListByPid(String id,String pid) {
		String sql = "select * from paper_item where parent_id = '"+id+"' and paper_id = '"+pid+"'";
		return PaperItem.dao.find(sql);
	}

	public List<PaperItem> getHistroyListByPid(String id,String pid){
		String sql = "select * from paper_user where parent_id = '"+id+"' and paper_id = '"+pid+"' and create_id = '"+ShiroKit.getUserId()+"'";
		return PaperItem.dao.find(sql);
	}

	public List<PaperItem> getRootList(String id){
		String sql = "select * from paper_item where parent_id ='#root' and paper_id = '"+id+"'";
		return PaperItem.dao.find(sql);
	}

}